import java.io.IOException;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TCDatabase {
	public Connection conn = null;
	public Statement sta = null;
	String url="jdbc:informix-sqli://172.28.2.11:8011/tcserver:INFORMIXSERVER=online;DB_LOCALE=zh_cn.gb;CLIENT_LOCALE=zh_cn.gb;DBDATE=mdy4;";
	String user="tcserver";
	String password="tcserver";
	String errMsg = null;
	public TCDatabase(){
		try {
			Class.forName("com.informix.jdbc.IfxDriver").newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			conn = DriverManager.getConnection(url,user,password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			sta = conn.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public boolean insertInfo(String getData,String _sys_Batch,int _serial){
		Date date = new Date();
		SimpleDateFormat sdfdate = new SimpleDateFormat("yyyyMMdd");
		String nowDate = sdfdate.format(date);
		SimpleDateFormat sdftime = new SimpleDateFormat("HHmmss");
		String nowTime = sdftime.format(date);
		
		String RecordNo = nowDate+"000001";
		String data[] = getData.split(",");
		String insertTcinfo = "insert into tc_data_info values ('"+data[0]+"','"+data[1]+"','"+data[2]+"','"+data[3]+"','"+data[4]+"','"+data[5]+"','"+nowDate+"','"+nowTime+"','"+_serial+"','"+_sys_Batch+"','0')";
		try {
			sta.executeUpdate(insertTcinfo);
			try {
				if(!this.insertSport(_sys_Batch,data[3],data[4],data[5],_serial)){
					this.deleteTableData(_sys_Batch);
					return false;
				};
			} catch (IOException e) {
				this.deleteTableData(_sys_Batch);
				return false;
			}
		} catch (SQLException e) {
			this.deleteTableData(_sys_Batch);
			return false;
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				
			}
		}
		return true;
	}
	public boolean insertSport (String _batch,String _accountno,String _amount,String _dealType,int _serial) throws IOException{
		Date sportDate = new Date();
		SimpleDateFormat sdfsportdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String sportnowDate = sdfsportdate.format(sportDate);
		try {
				TCSerial tcs = new TCSerial();
				String RecordNo = tcs.getRecordNo();
				String insertSport = "insert into sport_info (recordno,accountno,accountname,bankno,counterno,accounttype,dealtype,accountbalance,dealamount,dealtime,dealno,flag)" +
				"values ('"+RecordNo+"','"+_accountno+"','"+_accountno+"','sh pos',' ','Pos','"+_dealType+"','0','"+_amount+"','"+sportnowDate+"','2','0')";
				sta.executeUpdate(insertSport);
				String updateSport = "update tc_data_info set flag = 1 where sys_batch = '"+_batch+"' and sys_serial ='"+_serial+"'";
				sta.executeUpdate(updateSport);
				
				tcs.setRecordNo();
		
		} catch (SQLException e) {
			return false;
		} finally{
			try {
				conn.close();
			} catch (SQLException e) {
				return false;
			}
		}
		return true;
		
		
	} 
	public void deleteTableData(String Batch){
		String deleteTcinfo = "delete from tc_data_info where sys_batch = "+Batch+"";
		String deleteSport = "delete from sport_info";
		try {
			sta.executeUpdate(deleteTcinfo);
			sta.executeUpdate(deleteSport);
		} catch (SQLException e) {
			
		}
	}
}